#!/bin/bash

osscmd="/workspace/share/ota/script/osscmd"
vinciotadir="/workspace/share/ota/src"
osshost="oss-cn-beijing.aliyuncs.com"
$osscmd config --id='aDo4ZtbecPs4GrAh' --key='HO0xFyI8zRdGFV5KrH5oJgpG8qG5mY'

uploadfile() {
  if [ $checkok -eq 1 ]; then
    filename="ota_info.json"
    echo -e "\e[1;34mupload formal file to oss: $filename\e[0m"
  else
    filename="ota_info_ready.json"
  fi
  retry=0
  while [ $retry -lt 3 ];
  do
    if [ $retry -gt 0 ]; then
      echo -e "\e[1;31m retry times: $retry\e[0m"
    fi
    $osscmd put $file oss://inspero-rom-update-ossbucket/$filename --host=$osshost
    if [ $? -eq 0 ]; then
       return 0
    fi
    retry=$((retry+1))
  done
  return 1
}
file=$1

if [ ! -f $file ]; then
  echo -e "\e[1;31m$file not exist , exit\e[0m" >&2; exit 1
fi

if [ "x$2" = "xcheckok" ]; then
  checkok=1
fi

uploadfile
if [ $? -ne 0 ]; then
  echo -e "\e[1;31mupload $file error , exit\e[0m" >&2; exit 1
else
  echo -e "\e[1;34mota_info.json has uploaded\e[0m"
fi
